/**
 * 2018年12月5日下午12:12:45
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.PathKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.project.ds.common.CommonService;
import com.baihang.project.ds.entity.LouDongEntity;
import com.baihang.project.ds.service.RoomMonitorRedisService;
import com.baihang.project.ds.service.RoomMonitorService;

import redis.clients.jedis.Jedis;

/**
 * 房源监控
 *
 */
@Controller
@RequestMapping("/roomMonitor")
public class RoomMonitorController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(RoomMonitorController.class);
	private Jedis jedis = RedisKit.getJedis();
	@Autowired
	private RoomMonitorService service;
	@Autowired
	private RoomMonitorRedisService redisService;
	@Autowired
	private CommonService commonService;
	@RequestMapping
	public String index() {
		return "/project/ds/roomMonitor/index";
	}
	@RequestMapping("/loudongData")
	public ModelAndView loudongData(){
		ModelAndView  mav = new ModelAndView("/project/ds/roomMonitor/loudongData");
		List<LouDongEntity> list = commonService.getLouDong();
		System.err.println(list.size());
		mav.addObject("loudong",list);
		return mav;
	}
	
	/**
	 * 根据手机号码确认筹用户情况
	 * @param tel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/formByTel", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String formByTel(String tel) throws Exception {
		
		return service.formByTel(tel);
	}
	/**
	 * 预定，取消预定，退订 三种情况综合操作
	 * @param action 
	 * @param name
	 * @param tel
	 * @param roomid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/RoomGuideAjax", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String RoomGuideAjax(String action,String name,String tel,String cardno,int roomid) throws Exception {
		String result ="false" ;
		String msg ="操作失败";
		try {
			/*if(service.RoomGuideAjax(action,name,tel,cardno,roomid)) {
				result ="true";
				msg="操作成功";
			}*/
			if(redisService.RoomGuideAjax(action,name,tel,cardno,roomid)) {
				result ="true";
				msg="操作成功";
			}
		}catch (Exception e) {
			logger.info(e.getMessage());
		}
		return JsonKit.toDoubleJson(result, msg);
	}
	
	
	@RequestMapping(value ="/UploadRoomBackOrder", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String UploadRoomBackOrder(MultipartFile backorderlist) {
		String result="false";
		String msg="上传失败";
		String json="1";
		String filepath = PathKit.getWebRootPath()+"/upload/"+backorderlist.getOriginalFilename();
		System.err.println(filepath);
		try {
			backorderlist.transferTo(new File(filepath));
			json = service.readRoomBackOrderByExcel(backorderlist.getOriginalFilename());
			result="true";
			msg="上传成功";
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.format("{\"result\":\"%s\",\"json\":%s,\"msg\":\"%s\"}", result,json,msg);
	}
	
	@RequestMapping(value ="/RoomGuideAjaxAll", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String RoomGuideAjaxAll(String json) throws Exception {
		logger.info("json="+json);
		String result ="false" ;
		String msg ="操作失败";
		try {
			if(service.RoomGuideAjaxAll(json)) {
				result ="true";
				msg="操作成功";
			}
		}catch (Exception e) {
			logger.info(e.getMessage());
		}
		return JsonKit.toDoubleJson(result, msg);
	}
	
	/**
	 * 单个房间进行翻红操作
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/singleFanHong", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String singleFanHong(int id){
		String result ="true" ;
		//String msg ="操作成功";
		//service.singleFanHong(id);
		redisService.singleFanHong(id);
		return JsonKit.toDoubleJson2(result, String.valueOf(id));
	}
	/**
	 * 大批量翻红
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/moreFanHong", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String moreFanHong() throws Exception {
		String result ="true" ;
		//String msg ="操作成功";
		/*if("0".equals(service.moreFanHong())) {
			result ="false";
		}*/
		String msg = redisService.moreFanHong();
		if("0".equals(msg)) {
			result ="false";
		}
		return JsonKit.toDoubleJson2(result, msg);
	}
	
	/**
	 * 内控都还原到初始状态
	 * @return
	 */
	@RequestMapping(value ="/moreReset", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String moreReset() {
		String result ="false" ;
		String msg ="还原失败";
		/*if(service.moreReset()) {
			result="true";
			msg="还原成功";
		}*/
		if(redisService.moreReset()) {
			result="true";
			msg="还原成功";
		}
		return JsonKit.toDoubleJson(result, msg);
	}
	
	@RequestMapping(value ="/checkRoomState", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String checkRoomState(){
		String state = jedis.hmget("systemConfig","State").get(0);
		if("4".equals(state)) {//活动开启状态	
			return redisService.checkRoomState();			
		}
		return service.checkRoomState();
	}
	 

}
